package com.deyuanyun.pic.settings.service.prvlg.impl;

import com.deyuanyun.pic.settings.support.cache.SessionCacheSupport;
import com.deyuanyun.pic.common.cache.TempStoreDataSupport;
import com.deyuanyun.pic.common.util.ObjectUtil;
import com.deyuanyun.pic.common.util.web.Asserts;
import com.deyuanyun.pic.settings.controller.dto.UserSessionVO;
import com.deyuanyun.pic.settings.domain.base.Threshold;
import com.deyuanyun.pic.settings.domain.prvlg.Menu;
import com.deyuanyun.pic.settings.mapper.prvlg.CustomUserMapper;
import com.deyuanyun.pic.settings.mapper.prvlg.MenuMapper;
import com.deyuanyun.pic.settings.service.prvlg.MenuService;
import com.github.pagehelper.Page;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MenuServiceImpl implements MenuService {
    static Logger logger=Logger.getLogger(MenuServiceImpl.class);

	@Resource
	MenuMapper menuMapper;
	@Autowired
	CustomUserMapper customUserMapper;

	@Override
	public Page<Menu> queryMenuListByName(final String menuName) {
		return menuMapper.queryMenuListByName(menuName);
	}

	@Override
	public List<Menu> queryMenuList(String userId) {
		return menuMapper.queryMenuList(userId);
	}

    @Override
    public List<Menu> queryAllSubLevelMenu(String userId, String parentId){
        return menuMapper.queryAllSubLevelMenu(userId, parentId);
    }

    @Override
    public List<Menu> querySameLevelMenu(String userId, String parentId){
        return menuMapper.querySameLevelMenu(userId, parentId);
    }

	@Override
	public List<Menu> queryCurrUserMenu(){
		UserSessionVO sessionVO= SessionCacheSupport.getSessionVO();
		Asserts.assertTrue(sessionVO != null,"没有获取到sessionVO");
        Asserts.assertTrue(StringUtils.isNotBlank(sessionVO.getRoleIds()),"还没分配角色");
		String[] s= StringUtils.split(sessionVO.getRoleIds(),",");
		Map map=new HashMap();
		map.put("rolesId", s);
		List<String> menuIsds = customUserMapper.queryMenuIdByRoleIds(map);

		List<Menu> userMenuList=new ArrayList<Menu>();
		List<Menu> menuList = TempStoreDataSupport.pullDataf("application.menus");
        if (ObjectUtil.isEmptyAll(menuList)){
            logger.error(">>>出现异常，缓存中没有菜单，重新查询>>");
            menuList=querySameLevelMenu("","");
            TempStoreDataSupport.pushDataf("application.menus",menuList);
        }

		for (String mid:menuIsds){
			for (Menu m:menuList){
				if (mid.equals(m.getId())){
					userMenuList.add(m);
				}
			}
		}
		return userMenuList;
	}

	@Override
	public List<Threshold> queryThresholdGroupList(Map map){
		List<Threshold> thresholdList = TempStoreDataSupport.pullData("List_queryThresholdGroupList");
		if (ObjectUtil.isEmptyAll(thresholdList)){
			thresholdList =customUserMapper.queryThresholdGroupList(map);
			TempStoreDataSupport.pushDataByTimeMinute("List_queryThresholdGroupList", thresholdList,30);
		}
		return thresholdList;
	}


    @Override
    public List<Menu> getRootMenu(String userId){
        return menuMapper.querySameLevelMenu(userId, null);
    }

	public int deleteByPrimaryKey(BigDecimal id) {
		return 0;
	}

	public int insert(Menu record) {
		return 0;
	}

	public int insertSelective(Menu record) {
		return 0;
	}

	public Menu selectByPrimaryKey(BigDecimal id) {
		return null;
	}

	public int updateByPrimaryKeySelective(Menu record) {
		return 0;
	}

	public int updateByPrimaryKey(Menu record) {
		return 0;
	}
}
